import {nanoid} from "nanoid";

function getNextTaskCodeByEdge(edges, code) {
  // console.log('getNextTaskCodeByEdge', edges, code)
  let nextCode = []
  for (let index = 0; index < edges.length; index++) {
    let edge = edges[index]
    if (edge.source === code) {
      nextCode.push(edge.target)
    }
  }
  // console.log('next code', nextCode)

  return nextCode
}

function getTaskFromNode(nodes, code) {
  // console.log('get task', nodes, code)
  for (let index = 0; index < nodes.length; index++) {
    if (nodes[index].id === code) {
      // console.log('get task node', nodes[index], nodes[index].data)
      return nodes[index].data
    }
  }
}

function getFlowByCode(flow, code) {
  let workFlow = []
  let nextCodeList = [code]

  while (nextCodeList.length > 0) {
    // console.log('nextCodeList', nextCodeList)
    let taskCode = nextCodeList[0]
    let task = getTaskFromNode(flow.nodes, taskCode)
    // console.log('task', task)
    nextCodeList = getNextTaskCodeByEdge(flow.edges, taskCode)

    if (nextCodeList.length <= 1) {
      // 防止删除分支后遗留数据
      // console.log('task', task)
      if (task.item_list !== undefined) {
        delete task.item_list
      }

      // 如果是判断，检查是否有分支
      // const check_type = [101, 102, 105, 106]
      const check_type = [105]
      if (check_type.indexOf(task.type) > -1) {
        // console.log('need sub')
        const item_list = []
        item_list.push({
          code: nanoid(),
          content: '',
          prompt: '',
          type: 203,    // REDO
          flow: []
        })

        item_list.push({
          code: nanoid(),
          content: '',
          prompt: '',
          type: 202,    // 正确分支
          flow: getFlowByCode(flow, nextCodeList[0])
        })

        // 添加分支数据
        task.item_list = item_list
        workFlow.push(task)

        // 返回
        return workFlow
      }

      // 正常节点，直接压入队列
      workFlow.push(task)
    } else if (nextCodeList.length > 1) {
      // 分支节点，需要补充分支选项
      let item_list = []

      nextCodeList.forEach(function (taskCode, index) {
        let itemTask = getTaskFromNode(flow.nodes, taskCode)
        // TODO：应该判断是否是分支
        // console.log('item node', taskCode, itemTask)
        nextCodeList = getNextTaskCodeByEdge(flow.edges, itemTask.code)
        if (nextCodeList.length>0) {
          item_list.push({
            code: itemTask.code,
            content: itemTask.content,
            prompt: itemTask.prompt,
            type: itemTask.type,
            flow: getFlowByCode(flow, nextCodeList[0])
          })
        } else {
          // 如果是Redo分支，就没有后续流程
          item_list.push({
            code: itemTask.code,
            content: itemTask.content,
            prompt: itemTask.prompt,
            type: itemTask.type,
            flow: []
          })

        }
      })

      task.item_list = item_list
      workFlow.push(task)

      // 直接退出
      break
    }
  }

  return workFlow
}

export function formatCheckpointFlow(checkpoint) {
  // console.log('formatCheckpointFlow', checkpoint)
  if ((checkpoint.flow === undefined) || (checkpoint.flow.nodes === undefined)) {
    return
  }

  // 更新头节点名字
  checkpoint.flow.nodes.forEach(function (node){
    // console.log('checkpoint', checkpoint.name)
    // console.log('node', node)
    if (node.id === '0000') {
      node.label = checkpoint.name
    }
  })

  let nextCodeList = getNextTaskCodeByEdge(checkpoint.flow.edges, '0000')
  // console.log('nextCodeList', nextCodeList)
  if (nextCodeList.length === 1) {
    // 默认任务起始节点
    let workflow = getFlowByCode(checkpoint.flow, nextCodeList[0])
    console.log(checkpoint.name, 'work flow', workflow)
    checkpoint.workflow = {
      flow: workflow,
      step: 0,
    }
  }
}
